﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows.Media.Imaging;
using AutoLoL.Logic.Parsers;
using AutoLoL.Logic.Properties;
using AutoLoL.Logic.Utils;

namespace AutoLoL.Logic.Masteries.Importers
{
    public class SoloMidMasteryImporter : IMasteryImporter
    {
        public string Name
        {
            get { return "SoloMid.net"; }
        }

        public BitmapSource Icon
        {
            get { return ImageParser.BitmapToBitmapSource(Resources.SoloMid); }
        }

        public bool Select(string input)
        {
            return input.ToLower().Contains("solomid.net");
        }

        public string Import(string source)
        {
            try
            {
                source = source.ToLower();

                if (source.Contains("solomid.net/guides.php?g="))
                {
                    // Get the html text
                    string text = Web.GetWebText(source);

                    //---------Grab Summoners--------------
                    string prefix = "title=\"summoners/";
                    string sommoner1, sommoner2;

                    //Get the summoners section of the page
                    string sommoners = text.Substring(text.IndexOf("<div class=\"summoners\">"));
                    sommoners = sommoners.Substring(0, sommoners.IndexOf("</div>"));
                    //get the first sommoner spell
                    sommoners = sommoners.Substring(sommoners.IndexOf(prefix));
                    sommoner1 = sommoners.Substring(0, sommoners.IndexOf(".png\""));
                    sommoner1 = sommoner1.Replace(prefix, "");
                    //move up to the second spell
                    sommoners = sommoners.Substring(sommoners.IndexOf(".png\""));
                    //grab the second sommoner spell
                    sommoners = sommoners.Substring(sommoners.IndexOf(prefix));
                    sommoner2 = sommoners.Substring(0, sommoners.IndexOf(".png\""));
                    sommoner2 = sommoner2.Replace(prefix, "");

                    //---------Grab Masteries--------------
                    // Strip preceeding junk
                    prefix = "<div class=\"small_masteries2\">";
                    text = text.Substring(text.IndexOf(prefix));

                    string result = string.Empty;

                    while (true)
                    {
                        string mastery = "<span class=\"boxtext\" >";
                        text = text.Replace("data-max=\"1\"", "");
                        text = text.Replace("data-max=\"2\"", "");
                        text = text.Replace("data-max=\"3\"", "");
                        text = text.Replace("data-max=\"4\"", "");
                        int startIndex = text.IndexOf(mastery);

                        if (startIndex >= 0)
                        {
                            text = text.Substring(startIndex + mastery.Length);

                            int temp = 0;
                            if (int.TryParse(text.Substring(0, 1), out temp) && int.TryParse(text.Substring(2, 1), out temp) && text[1].Equals('/'))
                            {
                                result += text[0];
                            }
                            else
                            {
                                if (result.Length > 0)
                                    result = sommoner1 + ":" + sommoner2 + ":" + result;

                                return result;
                            }

                        }
                        else
                        {
                            if (result.Length > 0)
                                result = sommoner1 + ":" + sommoner2 + ":" + result;

                            return result;
                        }
                    }
                }
            }
            catch { if (Debugger.IsAttached) throw; }

            return null;
        }
    }
}